package cn.fxl.org.controller;

import cn.fxl.basic.annotation.PreAuthorize;
import cn.fxl.basic.pageList.GmResult;
import cn.fxl.basic.result.AjaxResult;
import cn.fxl.org.domain.Department;
import cn.fxl.org.query.DeptQuery;
import cn.fxl.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
@Api(value = "部门的API",description = "部门的CRUD")
public class DepartmentController {
    @Autowired
    private IDepartmentService iDepartmentServiceent;
    @GetMapping
    @ApiOperation("获取所有部门信息")

    public List<Department> getAll(){
        return iDepartmentServiceent.getAll();
    }
    @GetMapping("/{id}")
    @ApiOperation("获取一个部门信息")

    public Department getOne(@PathVariable("id") Long id){
     return    iDepartmentServiceent.getOne(id);
    }
    @PostMapping
    @ApiOperation("高级查询")

    public GmResult querPage(@RequestBody DeptQuery deptQuery){
      return   iDepartmentServiceent.Pagequery(deptQuery);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除部门")
    @PreAuthorize(name = "删除部门",sn = "department:delete")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            iDepartmentServiceent.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            return new AjaxResult(false,"删除失败");
        }
    }

    @PutMapping
    @ApiOperation("修改部门信息")
    @PreAuthorize(name = "保存部门",sn = "department:save")
    public AjaxResult saveAndUpdate(@RequestBody Department department){
        try {
            if(department.getId()==null){
                iDepartmentServiceent.save(department);
            }else {
                iDepartmentServiceent.update(department);
            }
            return new AjaxResult();
        } catch (Exception e) {
            return new AjaxResult(false,"操作失败");
        }
    }
    @PatchMapping
    @ApiOperation("批量删除")
    @PreAuthorize(name = "批量删除部门",sn = "department:pathchdel")
    public AjaxResult pathchdel(@RequestBody Long [] a){
        try {
            iDepartmentServiceent.pathchdel(a);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
          return   new AjaxResult(false,"删除失败");
        }
    }
    @GetMapping("/deptTree")
    @ApiOperation("获取部门树")
    @PreAuthorize(name = "获取部门",sn = "department:get")
    public List<Department> deptTree(){
       return iDepartmentServiceent.deptTree();
    }
}
